package com.sike.serviceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sike.mapper.GoodsMapper;
import com.sike.mapper.GoodsTypeMapper;
import com.sike.model.Param.GoodsSearchParam;
import com.sike.model.Param.UploadGoodsPicParam;
import com.sike.model.entity.Goods;
import com.sike.model.vo.GoodsVo;
import com.sike.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsTypeMapper goodsTypeMapper;

    @Override
    public Page<GoodsVo> getAllGoods(Integer pageNum, Integer pageSize) {
        Page<GoodsVo> page = new Page<>(pageNum,pageSize);
        IPage<GoodsVo> goodsVoIPage = goodsMapper.selectAllGoods(page);
        return (Page<GoodsVo>) goodsVoIPage;
    }

    @Override
    public Page<GoodsVo> queryGoods(GoodsSearchParam goodsSearchParam) {
        Page<GoodsVo> page = new Page<>(goodsSearchParam.getPageNum(),goodsSearchParam.getPageSize());
        IPage<GoodsVo> goodsVoIPage = goodsMapper.queryGoods(page, goodsSearchParam.getSearchTxt());
        return (Page<GoodsVo>) goodsVoIPage;
    }

    @Override
    public Goods uploadPic(UploadGoodsPicParam uploadGoodsPicParam) {
        Goods goods = goodsMapper.selectById(uploadGoodsPicParam.getGoodsId());
        if (!Objects.isNull(goods)) {
            goods.setImg(uploadGoodsPicParam.getImgBase64());
            if (goodsMapper.updateById(goods)==0) throw new RuntimeException("上传失败！");
            else return goods;
        }
        else throw new RuntimeException("不存在该商品!");
    }

}
